package com.springboot.system.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.system.domain.auth.Permission;
import com.springboot.system.service.PermissionService;
import com.springboot.common.algorithm.PermissionAlgorithm;
import com.springboot.core.log.annotation.Log;
import com.springboot.core.security.permission.CheckPermission;
import com.springboot.core.web.mvc.BaseRest;
import com.springboot.core.web.mvc.R;

/**
 * 权限接口
 * 
 * @ClassName: PermissionRest
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2019年10月21日 下午4:56:06
 *
 */
@RestController
@RequestMapping(value = "rest/permission")
public class PermissionRest extends BaseRest {

	@Autowired
	private PermissionService permissionService;

	/**
	 * 获取所有权限
	 * 
	 * @return
	 */
	@CheckPermission("permission:list")
	@RequestMapping(value = "all")
	public R queryAll() {
		return R.ok(PermissionAlgorithm.tree(permissionService.queryAll()));
	}

	/**
	 * 根据角色id获取对应的权限
	 *
	 * @param @param  roleId
	 * @param @return 设定文件
	 * @return ResponseResult 返回类型
	 *
	 */
	@RequestMapping(value = "queryByRole/{roleId}")
	public R queryByRole(@PathVariable("roleId") Integer roleId) {
		return R.ok(PermissionAlgorithm.tree(permissionService.selectByRole(new Integer[] { roleId })));
	}

	@CheckPermission("permission:add")
	@Log(value = "新增权限")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public R add(@RequestBody Permission permission) {
		permissionService.add(permission);
		return R.ok();
	}

	@CheckPermission("permission:edit")
	@Log(value = "修改权限")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public R update(@RequestBody Permission right) {
		permissionService.update(right);
		return R.ok();
	}

	@CheckPermission("permission:del")
	@Log(value = "删除权限")
	@RequestMapping(value = "delete")
	public R delete(@RequestParam("id") Integer id) {
		permissionService.delete(id);
		return R.ok();
	}

}
